﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PVP
{

    class CheckFile
    {
        private List<string> GSIfn = new List<string>();
        private List<string> NA2fn = new List<string>();

        public CheckFile(string appPath)
        {
            if (!Directory.Exists(PVP.dataPath))
            {
                MessageBox.Show("数据目录不存在！");
                return;
            }
            if (Directory.Exists(PVP.LeftGrpPath))
            {
                Directory.Delete(PVP.LeftGrpPath, true);
            }
            if (Directory.Exists(PVP.RightGrpPath))
            {
                Directory.Delete(PVP.RightGrpPath, true);
            }
            if (Directory.Exists(PVP.LeftToleranceGrpPath))
            {
                Directory.Delete(PVP.LeftToleranceGrpPath, true);
            }
            if (Directory.Exists(PVP.RightToleranceGrpPath))
            {
                Directory.Delete(PVP.RightToleranceGrpPath, true);
            }
            Directory.CreateDirectory(PVP.LeftGrpPath);
            Directory.CreateDirectory(PVP.RightGrpPath);
            Directory.CreateDirectory(PVP.LeftToleranceGrpPath);
            Directory.CreateDirectory(PVP.RightToleranceGrpPath);

            string[] files = Directory.GetFiles(PVP.dataPath);
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].EndsWith(".GSI", true, null))
                {
                    GSIfn.Add(files[i]);
                }
                if (files[i].EndsWith(".NA2", true, null))
                {
                    NA2fn.Add(files[i]);
                }
            }
            process();
        }

        private void process()
        {
            string sline = string.Empty;
            string[] lineCells = null;
            string path1 = string.Empty;
            string path2 = string.Empty;
            SortedDictionary<string, string> dict = new SortedDictionary<string, string>();

            foreach (string fn in GSIfn)
            {
                using (StreamReader sr = new StreamReader(fn))
                {
                    while ((sline = sr.ReadLine()) != null)
                    {
                        lineCells = sline.Split(',');
                        if (lineCells[0].StartsWith("8"))
                        {
                            path1 = PVP.LeftGrpPath;
                            path2 = PVP.LeftToleranceGrpPath;
                        }
                        if (lineCells[0].StartsWith("9"))
                        {
                            path1 = PVP.RightGrpPath;
                            path2 = PVP.RightToleranceGrpPath;
                        }
                        if (dict.ContainsKey(lineCells[0]))
                        {
                            dict[lineCells[0]] += "," + lineCells[1] + "," + lineCells[2];
                        }
                        else
                        {
                            dict.Add(lineCells[0], lineCells[1] + "," + lineCells[2]);
                        }
                    }
                }

                string fname = fn.Substring(fn.LastIndexOf('\\'));
                using (StreamWriter sw = new StreamWriter(path1 + fname))
                {
                    foreach (KeyValuePair<string, string> kvp in dict)
                    {
                        sw.WriteLine(kvp.Key + "," + kvp.Value);
                    }
                }

                using (StreamWriter sw = new StreamWriter(path2 + fname))
                {
                    string[] cells = null;
                    double sumx = 0.0;
                    double sumy = 0.0;
                    foreach (KeyValuePair<string, string> kvp in dict)
                    {
                        sumx = 0.0;
                        sumy = 0.0;
                        cells = kvp.Value.Split(',');
                        for (int j = 0; j < cells.Length; )
                        {
                            sumx += double.Parse(cells[j]);
                            sumy += double.Parse(cells[j + 1]);
                            j += 2;
                        }
                        sw.WriteLine(kvp.Key + "," + sumx / (cells.Length / 2) + "," + sumy / (cells.Length / 2));
                    }
                }

                dict.Clear();
            }
        }

    }
}
